package com.etc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.etc.entity.User;
import com.etc.service.LoginService;
import com.etc.utils.AddFace;
import com.etc.utils.CommonMessage;
import com.etc.utils.SearchFace;

@Controller
@RequestMapping(value = "adminapi")
public class LoginController {
	@Resource
	private LoginService loginservice;

	/**
	 * 商家和后台管理员登录的功能
	 * 
	 * @param utel
	 * @param upwd
	 * @param ustate
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "userLogin")
	public String userLogin(String utel, String upwd, int ustate, HttpSession session, HttpServletRequest request) {
		User user = loginservice.userLogin(utel, upwd, ustate);
		if (user != null) {
			session.setAttribute("user", user);
			return "admin/index";
		} else {
			if (ustate == 3) {
				return "redirect:../index-login3";
			} else {
				return "redirect:../index-login2";
			}

		}

	}

	@RequestMapping(value = "userexit")
	public String userexit(int ustate, HttpSession session) {
		session.removeAttribute("user");
		if (ustate == 2) {
			return "redirect:../index-login2";
		} else {
			return "redirect:../index-login3";
		}

	}

	@RequestMapping(value = "topersonal")
	public String toadminpersonal() {
		return "admin/adminpersonal";
	}

	@RequestMapping(value = "entryface")
	@ResponseBody
	public CommonMessage entryface(String image, int uid) {
		CommonMessage cm = new CommonMessage();

		System.out.println(image);
		// TODO Auto-generated method stub
		// String basePath = "upload\\";
		// String filePath = request.getSession().getServletContext().getRealPath("/") +
		// basePath;
		// String fileName = DateUtils.getDate("yyyyMMddHHmmss") + ".png";
		// String fileName = (new Date()).getTime() + ".png";
		// 默认传入的参数带类型等参数：data:image/png;base64,
		String imgStr = image;
		System.out.println(uid);
		if (null != imgStr) {
			imgStr = imgStr.substring(imgStr.indexOf(",") + 1);
		}
		// Boolean flag = GenerateImage(imgStr, filePath, fileName);
		String result = AddFace.add(imgStr, uid + "");
		JSONObject jo = JSONObject.parseObject(result);
		String str = (String) jo.get("error_msg");
		if (str.equals("SUCCESS")) {
			cm.setMsg("新增成功");

			// response.getWriter().println("新增成功");
		} else {
			cm.setMsg("新增失败");
			// response.getWriter().println("新增失败");
		}
		return cm;
	}

	@RequestMapping(value = "loginface")
	@ResponseBody
	public CommonMessage loginByface(String image, HttpSession session) {
		CommonMessage cm = new CommonMessage();
		// String basePath = "upload\\";
		// String filePath = request.getSession().getServletContext().getRealPath("/") +
		// basePath;
		// String fileName = DateUtils.getDate("yyyyMMddHHmmss") + ".png";
		// String fileName = (new Date()).getTime() + ".png";
		// 默认传入的参数带类型等参数：data:image/png;base64,

		// String id = request.getParameter("id");
		// System.out.println(id);
		String imgStr = image;
		if (null != imgStr) {
			imgStr = imgStr.substring(imgStr.indexOf(",") + 1);
		}
		// Boolean flag = GenerateImage(imgStr, filePath, fileName);
		String result = SearchFace.search(imgStr);
		JSONObject jo = JSONObject.parseObject(result);
		User user = new User();
		String error_msg = jo.getString("error_msg");
		// 如果SearchFace操作成功
		if (error_msg.equals("SUCCESS")) {
			String str = jo.getString("result");
			JSONObject rjo = JSONObject.parseObject(str);
			// 获取result结果中的user_list数组
			JSONArray jsonArray = rjo.getJSONArray("user_list");
			// 将JSONArray数组转为list集合
			List<String> list = new ArrayList<>();
			if (jsonArray != null) {
				for (int i = 0; i < jsonArray.size(); i++) {
					list.add(jsonArray.getString(i));
				}
			}
			// 获取user_list中的值
			JSONObject ujo = JSONObject.parseObject(list.get(0));
			Double score = ujo.getDouble("score");
			String user_id = ujo.getString("user_id");
			String[] arr = user_id.split("_");
			// 根据获取userId
			int uid = Integer.parseInt(arr[1]);
			
			if (score > 90) {
				// 根据id查询用户信息
				user = loginservice.faceLogin(uid);
				if(user!=null) {
				cm.setMsg("人脸存在");
				session.setAttribute("user", user);
				}else {
					cm.setMsg("没有该用户");
				}
			} else {
				cm.setMsg("人脸不存在");
			}

		} else {
			cm.setMsg("操作失败，请重新操作");
		}

		
		return cm;
	}
	@RequestMapping(value="faceLoginSuccess")
	public String faceLoginSuccess() {
		
		return "admin/index";
	}
}
